package com.study.samsung.Control;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.samsung.Entity.Commodity;
import com.study.samsung.Entity.ShoppingCart;
import com.study.samsung.Entity.Succession;
import com.study.samsung.Entity.User;
import com.study.samsung.Service.GoodsService;
import com.study.samsung.Service.IndexSearchService;
import com.study.samsung.Utils.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@WebServlet("/goods")
public class GoodsServlet extends BaseServlet {
    GoodsService gds = new GoodsService();
    IndexSearchService iss = new IndexSearchService();
    @Override
    public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");

        Object user = session.getAttribute("user");
        User nowuser = (User) user;

        if (nowuser==null){
            return "f:login";
        }else {
            Integer uid = nowuser.getUid();

            session.setAttribute("cartcount",gds.cartcount(uid));
            List<ShoppingCart> cartsgoods = gds.queryAllcart(nowuser.getUid());
            System.out.println(cartsgoods);
            if (cartsgoods.size()==0){
                request.setAttribute("cartsgoodsnull",1);
            }else {
                AtomicInteger sum= new AtomicInteger();
                cartsgoods.forEach(x->{
                    sum.addAndGet(x.getGnum());
                });
                request.setAttribute("cartsgoods",cartsgoods);
                request.setAttribute("sum",sum);
            }

            List<Commodity> newgoods = iss.selectnew();
            List<Succession> selectxl = iss.selectxl();

            request.setAttribute("newgoods",newgoods);
            request.setAttribute("selectxl",selectxl);
            return "f:goods";
        }
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String aftergnum = request.getParameter("aftergnum");
        String gid = request.getParameter("gid");

        gds.updategnum(Integer.parseInt(aftergnum), Integer.parseInt(gid));

    }

    public Integer deletecart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String gid = request.getParameter("gid");

        Integer deletecart = gds.deletecart(Integer.parseInt(gid));

        return deletecart;

    }

    public Integer piliangdel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String arr = request.getParameter("arr");
        JSONArray objects = JSONArray.parseArray(arr);
        int[] arrs = new int[objects.size()];

        for (int i = 0; i < arrs.length; i++) {
            arrs[i]= Integer.parseInt(objects.get(i).toString());
        }

        Integer piliangdel = gds.piliangdel(arrs);
        return piliangdel;
    }

    public JSONObject xiugai(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        JSONObject jsonObject = new JSONObject();
        String sxid = request.getParameter("sxid");
        session.setAttribute("oldsxid",sxid);
        List<Object> querycolormemory = gds.querycolormemory(Integer.parseInt(sxid));
        session.setAttribute("now_sid",querycolormemory.get(2));
        List<Object> nowcolormemory = gds.nowcolormemory(sxid);
        jsonObject.put("queryAllcm",querycolormemory);
        jsonObject.put("nowcm",nowcolormemory);
        return jsonObject;
    }

    public void xiugailast (HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String memoryValue = request.getParameter("memoryValue");
        String colorValue = request.getParameter("colorValue");
        Integer now_sid = (Integer) session.getAttribute("now_sid");
        Integer querynewsxid = gds.querynewsxid(memoryValue, colorValue, now_sid);
        session.setAttribute("querynewsxid",querynewsxid);
    }

    public Integer xiugaiyes(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        Integer querynewsxid = (Integer)session.getAttribute("querynewsxid");
        Integer oldsxid = Integer.parseInt(session.getAttribute("oldsxid").toString());
        Integer updatesxid = gds.updatesxid(querynewsxid, oldsxid);
        return updatesxid;
    }

    public String chuan(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");

        String arr = request.getParameter("arr");
        JSONArray objects = JSONArray.parseArray(arr);
        int[] arrs = new int[objects.size()];

        for (int i = 0; i < arrs.length; i++) {
            arrs[i]= Integer.parseInt(objects.get(i).toString());
        }

        List<ShoppingCart> cartsgoods = gds.chuanOrder(arrs);

        System.out.println(cartsgoods);
        session.setAttribute("cartsdata",cartsgoods);
        return "f:orderXq";
    }
}
